package org.example.library.controller;

import org.example.library.pojo.Book;
import org.example.library.pojo.User;
import org.example.library.pojo.dto.BookDto;
import org.example.library.pojo.dto.ResponseMessage;
import org.example.library.pojo.dto.UserDto;
import org.example.library.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    IBookService bookService;
    //增
    @PostMapping
    public ResponseMessage<Book> add(@Validated @RequestBody BookDto book) {
        Book bookNew = bookService.add(book);
        return ResponseMessage.success(bookNew);
    }
        //删
    @DeleteMapping("/{bookId}")
    public ResponseMessage<Book> delete(@PathVariable Integer bookId) {
        Book bookNew = bookService.delete(bookId);
        return ResponseMessage.success(bookNew);
    }


    //改
    @PutMapping
    public ResponseMessage<Book> update(@Validated @RequestBody BookDto book) {
        Book bookNew = bookService.update(book);
        return ResponseMessage.success(bookNew);
    }

        //查
    @GetMapping("/{bookId}")
    public ResponseMessage<Book> get(@PathVariable Integer bookId) {
        Book bookNew = bookService.getBook(bookId);
        return ResponseMessage.success(bookNew);
    }


}
